package com.ethink.platform.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.constant.OrderRespCode;
import com.ethink.order.api.model.MateInventorySaleChannelChangeLogDetailExcelImportModel;
import com.ethink.platform.controller.order.info.MateInventorySaleChannelChangeLogDetailImportInfo;
import com.ethink.warehouse.api.MateInventoryFacade;
import com.ethink.warehouse.api.info.MateInventoryDetailInfo;
import com.ethink.warehouse.api.request.MateInventoryQueryKeyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @Author: lzy
 * @Date: Created in 2023/2/1 16:00
 * ModifiedBy:
 * Version:
 */
@Component
public class MateInventorySaleChannelChangeLogManager {
    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private MateInventoryFacade mateInventoryFacade;

    public List<MateInventorySaleChannelChangeLogDetailImportInfo> importMateInventorySaleChannelChangeLogDetail(
            String subWarehouseCode,
            String saleSubChannel,
            MultipartFile file
    ) {
        List<MateInventorySaleChannelChangeLogDetailExcelImportModel> excelRecordList = excelImportManager.transferClass(file, MateInventorySaleChannelChangeLogDetailExcelImportModel.class);
        List<MateInventoryQueryKeyInfo> mateInventoryQueryKeyInfoList = new ArrayList<>();
        excelRecordList.forEach(item -> {
            MateInventoryQueryKeyInfo mateInventoryQueryKeyInfo = new MateInventoryQueryKeyInfo();
            mateInventoryQueryKeyInfo.setSubWarehouseCode(subWarehouseCode);
            mateInventoryQueryKeyInfo.setSaleSubChannel(saleSubChannel);
            mateInventoryQueryKeyInfo.setInventoryType(item.getInventoryType());
            mateInventoryQueryKeyInfo.setMateSku(item.getMateSku());
            mateInventoryQueryKeyInfo.setPurchaseCode(item.getPurchaseCode());
            mateInventoryQueryKeyInfoList.add(mateInventoryQueryKeyInfo);
        });

        List<MateInventoryDetailInfo> mateInventoryDetailInfoList = mateInventoryFacade.listMateInventoryByKeyList(mateInventoryQueryKeyInfoList);
        if (excelRecordList.size() != mateInventoryDetailInfoList.size()) {
            throw new BusinessException(OrderRespCode.MATE_SKU_EXCEL_IS_NOT_EXIST);
        }

        return excelRecordList
                .stream()
                .map(item -> {
                        MateInventorySaleChannelChangeLogDetailImportInfo importInfo = new MateInventorySaleChannelChangeLogDetailImportInfo();
                        importInfo.setQty(item.getQty());
                        importInfo.setExceptCostPrice(item.getExceptCostPrice());
                        importInfo.setExceptSaleTime(item.getExceptSaleTime());
                        mateInventoryDetailInfoList
                                .stream()
                                .filter(item1 -> StrUtil.equals(item.getMateSku(), item1.getMateSku()) && StrUtil.equals(item.getPurchaseCode(), item1.getPurchaseCode()))
                                .forEach(item2 -> {
                                            BeanUtil.copyProperties(item2, importInfo);
                                            importInfo.setMateInventoryId(item2.getId());
                                        }
                                );
                        return importInfo;
                    }
                ).collect(Collectors.toList());
    }
}
